<!--
  智慧社区管理系统 - 安防记录管理页面
  @author Wu.Liang
  @date 2025-01-20
  @description 安防事件记录、报警记录、访问记录管理
-->
<template>
  <div class="security-record-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-title">
          <el-icon size="24" class="title-icon"><Document /></el-icon>
          <h2>安防记录管理</h2>
        </div>
        <div class="header-actions">
          <el-button type="primary" @click="handleExport">
            <el-icon><Download /></el-icon>
            导出记录
          </el-button>
          <el-button @click="handleRefresh">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="statistics-cards">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card total-records">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Document /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.totalRecords }}</div>
                <div class="stat-label">记录总数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card alarm-records">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Warning /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.alarmRecords }}</div>
                <div class="stat-label">报警记录</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card access-records">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><User /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.accessRecords }}</div>
                <div class="stat-label">访问记录</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card today-records">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Calendar /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.todayRecords }}</div>
                <div class="stat-label">今日记录</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 搜索表单 -->
    <el-card class="search-card">
      <el-form :model="searchForm" :inline="true" class="search-form">
        <el-form-item label="记录类型">
          <el-select
            v-model="searchForm.recordType"
            placeholder="请选择记录类型"
            clearable
            style="width: 150px"
          >
            <el-option label="报警记录" :value="1" />
            <el-option label="访问记录" :value="2" />
            <el-option label="异常记录" :value="3" />
            <el-option label="系统记录" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="处理状态">
          <el-select
            v-model="searchForm.handleStatus"
            placeholder="请选择处理状态"
            clearable
            style="width: 150px"
          >
            <el-option label="未处理" :value="0" />
            <el-option label="处理中" :value="1" />
            <el-option label="已处理" :value="2" />
            <el-option label="已忽略" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="设备位置">
          <el-input
            v-model="searchForm.deviceLocation"
            placeholder="请输入设备位置"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="searchForm.timeRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 350px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 记录列表 -->
    <el-card class="record-list-card">
      <template #header>
        <div class="card-header">
          <span>安防记录列表</span>
          <div class="header-actions">
            <el-button type="success" size="small" @click="handleBatchHandle">
              <el-icon><Check /></el-icon>
              批量处理
            </el-button>
            <el-button type="warning" size="small" @click="handleBatchIgnore">
              <el-icon><Close /></el-icon>
              批量忽略
            </el-button>
          </div>
        </div>
      </template>

      <el-table
        :data="recordList"
        v-loading="loading"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="recordType" label="记录类型" width="120">
          <template #default="{ row }">
            <el-tag :type="getRecordTypeTag(row.recordType)">
              {{ getRecordTypeName(row.recordType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="deviceName" label="设备名称" min-width="150">
          <template #default="{ row }">
            <div class="device-name">
              <el-icon class="device-icon"><Monitor /></el-icon>
              <span>{{ row.deviceName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="deviceLocation" label="设备位置" min-width="150" />
        <el-table-column prop="eventDescription" label="事件描述" min-width="200" />
        <el-table-column prop="handleStatus" label="处理状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getHandleStatusTag(row.handleStatus)">
              {{ getHandleStatusName(row.handleStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="priority" label="优先级" width="100">
          <template #default="{ row }">
            <el-tag :type="getPriorityTag(row.priority)">
              {{ getPriorityName(row.priority) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="发生时间" width="180" />
        <el-table-column prop="handleTime" label="处理时间" width="180" />
        <el-table-column prop="handleUser" label="处理人" width="120" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleView(row)">
              <el-icon><View /></el-icon>
              查看
            </el-button>
            <el-button type="success" size="small" @click="handleProcess(row)">
              <el-icon><Check /></el-icon>
              处理
            </el-button>
            <el-button type="warning" size="small" @click="handleIgnore(row)">
              <el-icon><Close /></el-icon>
              忽略
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 记录详情弹窗 -->
    <el-dialog
      v-model="recordDetailVisible"
      title="安防记录详情"
      width="800px"
      :close-on-click-modal="false"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="记录类型">{{ getRecordTypeName(currentRecord.recordType) }}</el-descriptions-item>
        <el-descriptions-item label="设备名称">{{ currentRecord.deviceName }}</el-descriptions-item>
        <el-descriptions-item label="设备位置">{{ currentRecord.deviceLocation }}</el-descriptions-item>
        <el-descriptions-item label="处理状态">
          <el-tag :type="getHandleStatusTag(currentRecord.handleStatus)">
            {{ getHandleStatusName(currentRecord.handleStatus) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="优先级">
          <el-tag :type="getPriorityTag(currentRecord.priority)">
            {{ getPriorityName(currentRecord.priority) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="发生时间">{{ currentRecord.createTime }}</el-descriptions-item>
        <el-descriptions-item label="处理时间">{{ currentRecord.handleTime || '未处理' }}</el-descriptions-item>
        <el-descriptions-item label="处理人">{{ currentRecord.handleUser || '未处理' }}</el-descriptions-item>
        <el-descriptions-item label="IP地址">{{ currentRecord.ipAddress }}</el-descriptions-item>
        <el-descriptions-item label="MAC地址">{{ currentRecord.macAddress }}</el-descriptions-item>
        <el-descriptions-item label="事件描述" :span="2">{{ currentRecord.eventDescription }}</el-descriptions-item>
        <el-descriptions-item label="处理备注" :span="2">{{ currentRecord.handleRemark || '暂无备注' }}</el-descriptions-item>
      </el-descriptions>

      <template #footer>
        <el-button @click="recordDetailVisible = false">关闭</el-button>
        <el-button type="success" @click="handleProcess(currentRecord)">处理记录</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { showMessage } from '@/utils/message'
import {
  Document,
  Download,
  Refresh,
  Warning,
  User,
  Calendar,
  Search,
  Check,
  Close,
  View,
  Monitor
} from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(false)
const recordDetailVisible = ref(false)
const selectedRecords = ref<any[]>([])

// 搜索表单
const searchForm = reactive({
  recordType: undefined as number | undefined,
  handleStatus: undefined as number | undefined,
  deviceLocation: '',
  timeRange: [] as string[]
})

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 20,
  total: 0
})

// 统计数据
const statistics = reactive({
  totalRecords: 2847,
  alarmRecords: 156,
  accessRecords: 2456,
  todayRecords: 89
})

// 当前记录详情
const currentRecord = ref<any>({})

// 记录列表数据
const recordList = ref([
  {
    id: 1,
    recordType: 1,
    deviceName: '前门监控摄像头',
    deviceLocation: '小区前门',
    eventDescription: '检测到异常人员徘徊，触发报警',
    handleStatus: 0,
    priority: 2,
    createTime: '2025-01-20 15:30:25',
    handleTime: null,
    handleUser: null,
    ipAddress: '192.168.1.100',
    macAddress: '00:11:22:33:44:55',
    handleRemark: null
  },
  {
    id: 2,
    recordType: 2,
    deviceName: '1号楼门禁',
    deviceLocation: '1号楼单元门',
    eventDescription: '住户张三正常刷卡进入',
    handleStatus: 2,
    priority: 1,
    createTime: '2025-01-20 15:28:10',
    handleTime: '2025-01-20 15:28:15',
    handleUser: '系统自动处理',
    ipAddress: '192.168.1.101',
    macAddress: '00:11:22:33:44:56',
    handleRemark: '正常访问记录'
  },
  {
    id: 3,
    recordType: 3,
    deviceName: '地下车库报警器',
    deviceLocation: '地下车库',
    eventDescription: '检测到烟雾浓度异常，触发火灾报警',
    handleStatus: 1,
    priority: 3,
    createTime: '2025-01-20 14:15:30',
    handleTime: null,
    handleUser: '李四',
    ipAddress: '192.168.1.102',
    macAddress: '00:11:22:33:44:57',
    handleRemark: '已通知消防部门，正在处理中'
  },
  {
    id: 4,
    recordType: 4,
    deviceName: '物业办公室对讲',
    deviceLocation: '物业办公室',
    eventDescription: '系统自动巡检完成，设备状态正常',
    handleStatus: 2,
    priority: 1,
    createTime: '2025-01-20 15:25:45',
    handleTime: '2025-01-20 15:25:50',
    handleUser: '系统自动处理',
    ipAddress: '192.168.1.103',
    macAddress: '00:11:22:33:44:58',
    handleRemark: '系统自动处理'
  },
  {
    id: 5,
    recordType: 1,
    deviceName: '后门监控摄像头',
    deviceLocation: '小区后门',
    eventDescription: '检测到车辆异常停留，超过规定时间',
    handleStatus: 3,
    priority: 2,
    createTime: '2025-01-20 12:30:15',
    handleTime: '2025-01-20 12:35:20',
    handleUser: '王五',
    ipAddress: '192.168.1.104',
    macAddress: '00:11:22:33:44:59',
    handleRemark: '经核实为业主车辆，已忽略'
  }
])

// 获取记录类型标签样式
const getRecordTypeTag = (type: number): 'danger' | 'success' | 'warning' | 'info' => {
  const typeMap: Record<number, 'danger' | 'success' | 'warning' | 'info'> = {
    1: 'danger',   // 报警记录
    2: 'success',  // 访问记录
    3: 'warning',  // 异常记录
    4: 'info'      // 系统记录
  }
  return typeMap[type] || 'info'
}

// 获取记录类型名称
const getRecordTypeName = (type: number): string => {
  const typeMap: Record<number, string> = {
    1: '报警记录',
    2: '访问记录',
    3: '异常记录',
    4: '系统记录'
  }
  return typeMap[type] || '未知类型'
}

// 获取处理状态标签样式
const getHandleStatusTag = (status: number): 'danger' | 'warning' | 'success' | 'info' => {
  const statusMap: Record<number, 'danger' | 'warning' | 'success' | 'info'> = {
    0: 'danger',   // 未处理
    1: 'warning',  // 处理中
    2: 'success',  // 已处理
    3: 'info'      // 已忽略
  }
  return statusMap[status] || 'danger'
}

// 获取处理状态名称
const getHandleStatusName = (status: number): string => {
  const statusMap: Record<number, string> = {
    0: '未处理',
    1: '处理中',
    2: '已处理',
    3: '已忽略'
  }
  return statusMap[status] || '未知状态'
}

// 获取优先级标签样式
const getPriorityTag = (priority: number): 'info' | 'warning' | 'danger' => {
  const priorityMap: Record<number, 'info' | 'warning' | 'danger'> = {
    1: 'info',     // 低
    2: 'warning',  // 中
    3: 'danger'    // 高
  }
  return priorityMap[priority] || 'info'
}

// 获取优先级名称
const getPriorityName = (priority: number): string => {
  const priorityMap: Record<number, string> = {
    1: '低',
    2: '中',
    3: '高'
  }
  return priorityMap[priority] || '未知'
}

// 搜索处理
const handleSearch = () => {
  pagination.currentPage = 1
  loadRecordList()
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    recordType: undefined,
    handleStatus: undefined,
    deviceLocation: '',
    timeRange: []
  })
  handleSearch()
}

// 加载记录列表
const loadRecordList = () => {
  loading.value = true
  // 模拟API调用
  setTimeout(() => {
    loading.value = false
    pagination.total = recordList.value.length
  }, 500)
}

// 导出记录
const handleExport = () => {
  showMessage({ type: 'info', message: '导出功能开发中' })
}

// 刷新
const handleRefresh = () => {
  loadRecordList()
  showMessage({ type: 'success', message: '数据已刷新' })
}

// 查看记录详情
const handleView = (row: any) => {
  currentRecord.value = { ...row }
  recordDetailVisible.value = true
}

// 处理记录
const handleProcess = (row: any) => {
  showMessage({ type: 'info', message: '处理记录功能开发中' })
}

// 忽略记录
const handleIgnore = (row: any) => {
  showMessage({ type: 'info', message: '忽略记录功能开发中' })
}

// 批量处理
const handleBatchHandle = () => {
  if (selectedRecords.value.length === 0) {
    showMessage({ type: 'warning', message: '请选择要处理的记录' })
    return
  }
  showMessage({ type: 'info', message: '批量处理功能开发中' })
}

// 批量忽略
const handleBatchIgnore = () => {
  if (selectedRecords.value.length === 0) {
    showMessage({ type: 'warning', message: '请选择要忽略的记录' })
    return
  }
  showMessage({ type: 'info', message: '批量忽略功能开发中' })
}

// 选择变化处理
const handleSelectionChange = (selection: any[]) => {
  selectedRecords.value = selection
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  pagination.currentPage = 1
  loadRecordList()
}

// 当前页变化
const handleCurrentChange = (page: number) => {
  pagination.currentPage = page
  loadRecordList()
}

// 页面初始化
onMounted(() => {
  loadRecordList()
})
</script>

<style scoped lang="scss">
.security-record-container {
  padding: 20px;
  background: #f5f7fa;
  min-height: calc(100vh - 84px);

  .page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 20px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);

    .header-content {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .header-title {
        display: flex;
        align-items: center;
        color: white;

        .title-icon {
          margin-right: 12px;
        }

        h2 {
          margin: 0;
          font-size: 24px;
          font-weight: 600;
        }
      }

      .header-actions {
        display: flex;
        gap: 12px;
      }
    }
  }

  .statistics-cards {
    margin-bottom: 20px;

    .stat-card {
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      transition: transform 0.3s ease;

      &:hover {
        transform: translateY(-2px);
      }

      .stat-content {
        display: flex;
        align-items: center;
        padding: 8px;

        .stat-icon {
          margin-right: 16px;
          padding: 12px;
          border-radius: 8px;
          color: white;
        }

        .stat-info {
          .stat-number {
            font-size: 28px;
            font-weight: bold;
            line-height: 1;
            margin-bottom: 4px;
          }

          .stat-label {
            font-size: 14px;
            color: #666;
          }
        }
      }

      &.total-records .stat-icon {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      }

      &.alarm-records .stat-icon {
        background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
      }

      &.access-records .stat-icon {
        background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
      }

      &.today-records .stat-icon {
        background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
      }
    }
  }

  .search-card {
    margin-bottom: 20px;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    .search-form {
      .el-form-item {
        margin-bottom: 16px;
      }
    }
  }

  .record-list-card {
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-weight: 600;
      font-size: 16px;

      .header-actions {
        display: flex;
        gap: 8px;
      }
    }

    .device-name {
      display: flex;
      align-items: center;

      .device-icon {
        margin-right: 8px;
        color: #409eff;
      }
    }

    .pagination-container {
      display: flex;
      justify-content: center;
      margin-top: 20px;
      padding-top: 20px;
      border-top: 1px solid #ebeef5;
    }
  }
}
</style> 